package cn.wolfcode.shop.cloud.service.impl;

import cn.wolfcode.shop.cloud.domain.SeckillOrder;
import cn.wolfcode.shop.cloud.mapper.SeckillOrderMapper;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.service.ISeckillOrderService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SeckillOrderServiceImpl implements ISeckillOrderService {
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public SeckillOrder queryByUserIdAndSeckillId(Long userId, Long SeckillId) {
        return seckillOrderMapper.queryByUserIdAndSeckillId(userId, SeckillId);
    }

    @Override
    public void createSeckillOrder(Long userId, String orderNo, Long seckillId) {
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(userId);
        seckillOrder.setOrderNo(orderNo);
        seckillOrder.setSeckillId(seckillId);
        seckillOrderMapper.save(seckillOrder);

        //往redis中存一条数据
        redisService.set(SeckillKeyPrefix.SECKILL_CREATE_ORDER, userId + ":" + seckillId, seckillOrder);
    }
}
